import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

public class FootBallTest {

    private List<Player> players;
    private MenuCommand menuCommand;
    private Map<String, MenuCommand> commandsList;
    private PrintStream printStream;
    private InputStreamReader inputStreamReader;
    private BufferedReader bufferedReader;
    private FootBall footBall;
    private ListPlayersCommand listPlayersCommand;
    private BufferedReader bufferedReader1;

    @Before
    public void setUp() throws Exception {

        listPlayersCommand = mock(ListPlayersCommand.class);
        commandsList = new HashMap<String, MenuCommand>();
        commandsList.put("1", listPlayersCommand);
        players = newArrayList(new Player("Player1", new Team("bears"), 12, 36));
        printStream = mock(PrintStream.class);
        inputStreamReader = mock(InputStreamReader.class);
        bufferedReader = new BufferedReader(inputStreamReader);
        footBall = new FootBall(players, commandsList, printStream, bufferedReader);
        bufferedReader1 = mock(BufferedReader.class);
    }

    @Test
    public void shouldDisplayMenuWhenRunIsCalled() throws IOException {

       footBall.displayMenu();
       verify(printStream).println("Press 1 for List Players");
       verify(printStream).println("Press 2 for Find Players");
    }

    @Test
    public void shouldGetUserInputWhenRetrieveMethodIsCalled() throws IOException {

        FootBall footBall1 = new FootBall(players, commandsList, printStream, bufferedReader1);
        when(bufferedReader1.readLine()).thenReturn("1");
        assertThat(footBall1.retrieveUserInputForMenuOption(), is("1"));


    }
//    test setup now failing- a null is returned when a mock should take care of it

//    @Test (expected = IOException.class)
//    public void shouldThrowExceptionWhenRetrieveMethodIsCalled() throws IOException {
//        when(inputStream.read()).thenThrow(IOException.class);
//        footBall.run();
//    }

    @Test
    public void shouldExecuteCommandWhenUserInputIs1() throws IOException {

        footBall.executeCommand("1");
//        verify(listPlayersCommand).act();
        verify(listPlayersCommand, atLeastOnce()).act();
    }


}
